﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace DigibizTree.Picker
{
    /// <summary>
    ///  A helper class to store and retrieve cookie values for the DAMP cookie.
    /// </summary>
    /// <remarks>
    /// The cookie is used to persist values from the client to the server since 
    /// it is much more complicated to try to persist these values between ajax request, 
    /// given the tree's current architecture.
    /// </remarks>
    public static class DigibizHttpCookieExtensions
    {
        internal static void AddStartNodeIdForDataType(this HttpCookie c, int id, int startNodeId)
        {
            c["STARTNODE_" + id] = startNodeId.ToString();
        }

        internal static int GetStartNodeIdForDataType(this HttpCookie c, int id)
        {
            //If the value hasn't been retreived from the request.
            if (c.Values.Keys.Cast<string>().Where(x => x == "STARTNODE_" + id.ToString()).Count() > 0)
            {
                return int.Parse(c.Values["STARTNODE_" + id.ToString()]);
            }
            return -1;
            
        }
    }
}
